﻿using System;
using System.Collections.Generic;
using Foothill.IoC;
using Foothill.Model;
using Foothill.Service.AdService;
using System.Web.UI.WebControls;
using Foothill.Common;
using System.IO;

namespace Foothill.Web.Admin
{
    public partial class PostManagement : FoothillBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        private void InitAdList()
        {
            
        }

        protected void BtnSearchIdClick(object sender, EventArgs e)
        {
            IAdminSearchService adminSearchService = IocContainer.GetInstance<IAdminSearchService>();
            string input = TbxSearchId.Text.Trim();

            if (string.IsNullOrWhiteSpace(input))
            {
                return;
            }
            
            IList<ForSaleAd> resultSet = adminSearchService.GetPostsByIds(input);
            gvwAdList.DataSource = resultSet;
            gvwAdList.DataBind();
        }

        protected void gvwAdListRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ForSaleAd post = e.Row.DataItem as ForSaleAd;

                DisplayAdInfo(e.Row, post);
                DisplayProfilePicture(e.Row, post);
            }
        }

        private void DisplayAdInfo(GridViewRow row, ForSaleAd post)
        {
            Label label = row.FindControl("lblVehicleType") as Label;
            label.Text = post.GetVehicleTypeString();
            
            string url = string.Format("~/ForSaleAdDetail/ForSaleAdDetail.aspx?{0}={1}&{2}=1", GlobalQueryString.AdDetailsQsName, post.AdId, GlobalQueryString.FromSearchListQsName);
            HyperLink link = row.FindControl("hlkAdTitle") as HyperLink;
            link.NavigateUrl = url;

            link = row.FindControl("hlkProfilePic") as HyperLink;
            link.NavigateUrl = url;

            label = row.FindControl("lblAdId") as Label;
            label.Text = post.AdId.ToString();
        }

        private void DisplayProfilePicture(GridViewRow row, ForSaleAd post)
        {
            Image imgProfile = row.FindControl("imgProfile") as Image;

            if (imgProfile != null)
            {
                // if there is no profile photo, the ProfilePhotoPath will be empty
                if (!string.IsNullOrEmpty(post.ProfilePhotoPath))
                {
                    string path = Path.Combine(GlobalSettings.ThumbnailFolder, Path.GetFileName(post.ProfilePhotoPath));

                    if (File.Exists(Server.MapPath(path)))
                    {
                        imgProfile.ImageUrl = path;
                        return;
                    }
                }

                // display "Photo Not Available" 
                imgProfile.ImageUrl = GlobalSettings.PhotoNotAvailablePath;
            }
        }
    }
}